/*
 * @Author: zhangyujie
 * @Date:   2015-08-12 10:45:39
 * @Last Modified by:   zhangyujie
 * @Last Modified time: 2015-08-14 13:56:00
 */

'use strict';

(function() {

	var Y = window.Y || {}

	var util = {

		getNumByRate: function(w, rate) {

			if (typeof rate === 'string' && rate.indexOf('%')) {

				return w * Number(rate.replace('%', '')) / 100
			} else if (typeof rate === 'string' && rate.indexOf('px')) {

				return Number(rate.replace('px', ''))
			} else {

				return Number(rafte)
			}
		},
		getTextSize: function(text, style) {

			var span, position, width, height

			span = document.createElement('span')

			span.innerText = text

			for (var key in style) {

				span.style[key] = style[key]
			}

			document.body.appendChild(span)

			position = span.getBoundingClientRect()

			width = position.width

			height = position.height

			document.body.removeChild(span)

			return {

				width: width,
				height: height
			}
		}
	}


	Y.seven = function(setting) {

		var selector = setting.selector,
			series = setting.series,
			colorset = setting.colorset,
			margin = setting.margin,
			nameStyle = setting.nameStyle,
			valueStyle = setting.valueStyle,
			width = document.querySelector(selector).offsetWidth,
			height = document.querySelector(selector).offsetHeight,
			innerRadius = util.getNumByRate(width, setting.radius[0]),
			outerRadius = util.getNumByRate(width, setting.radius[1]),
			startAngle = 0,
			endAngle = 2 * Math.PI,
			arcArray = [],
			lineArray = [],
			lineArrayLight = [],
			lineArrayBold = [],
			sum = 0,
			diffDistance = setting.diffDistance.length,
			lightLine = setting.lightLine.length,
			boldLine = setting.boldLine.length,

			title = setting.title,
			text = title.text,
			subtext = title.subtext,
			textStyle = title.textStyle,
			subtextStyle = title.subtextStyle,
			textRect = util.getTextSize(text, textStyle),
			subtextRect = util.getTextSize(text, subtextStyle)


		for (var i = 0; i < series.length; i++) {

			sum += series[i].value
		}

		var svg = d3.select(selector)
			.append('svg')
			.attr('width', width)
			.attr('height', height)

		var pie = d3.layout.pie()
			.sort(null)
			.startAngle(startAngle)
			.endAngle(endAngle)
			.value(function(d) {

				return parseFloat(d.value)
			})

		var con = svg.append('g')
			.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')')

		var line = con.append('g'),
			lineLight = con.append('g'),
			lineBold = con.append('g'),
			textValue = con.append('g'),
			textName = con.append('g')

		var arc = d3.svg.arc()
			.innerRadius(innerRadius)
			// .outerRadius(outerRadius)
			.outerRadius(function(d) {

				var value = parseFloat(d.data.value)
				d.r = (outerRadius + innerRadius) / 2

				arcArray.push(d)

				return outerRadius //innerRadius + (radius) * (value / 100.0)
			})



		var path = con.selectAll('.solidArc')
			.data(pie(series))
			.enter().append('path')
			.attr('fill', function(d, i) {

				return colorset[i]
			})
			.attr('class', 'solidArc')
			.attr('d', arc)

		// 添加辅助线
		for (var i = 0, len = arcArray.length; i < len; i++) {

			var x, y, diffAngle, x1, y1, x2, x3

			diffDistance = diffDistance

			diffAngle = (arcArray[i].startAngle + arcArray[i].endAngle) / 2

			x = Math.cos(diffAngle - Math.PI / 2) * (arcArray[i].r)

			y = Math.sin(diffAngle - Math.PI / 2) * (arcArray[i].r)

			x1 = Math.cos(diffAngle - Math.PI / 2) * (arcArray[i].r + diffDistance)

			y1 = Math.sin(diffAngle - Math.PI / 2) * (arcArray[i].r + diffDistance)

			if (x1 > 0) {

				x2 = x1 + lightLine
				x3 = x2 + boldLine
			} else {

				x2 = x1 - lightLine
				x3 = x2 - boldLine
			}

			var temp = {
				x: x,
				y: y,
				x1: x1,
				y1: y1,
				color: colorset[i]
			}

			var parm = {
				x: x1,
				y: y1,
				x1: x2,
				y1: y1,
				color: colorset[i],
				name: arcArray[i].data.name,
				value: ((arcArray[i].data.value * 100 / sum).toFixed(0))

			}

			var parm1 = {
				x: x2,
				y: y1,
				x1: x3,
				y1: y1,
				color: colorset[i],
				name: arcArray[i].data.name,
				value: ((arcArray[i].data.value * 100 / sum).toFixed(0))
			}

			lineArray.push(temp)

			lineArrayLight.push(parm)

			lineArrayBold.push(parm1)
		}

		con.selectAll('.line')
			.data(lineArray)
			.enter()
			.append('line')
			.attr('class', 'line')
			.attr('x1', function(d, i) {

				return d.x
			})
			.attr('y1', function(d, i) {

				return d.y
			})
			.attr('x2', function(d, i) {

				return d.x1
			})
			.attr('y2', function(d, i) {

				return d.y1
			})
			.attr('stroke', function(d, i) {

				return d.color
			})
			.style('stroke-width', '1')

		lineLight.selectAll('.lightLine')
			.data(lineArrayLight)
			.enter()
			.append('line')
			.attr('class', 'lightLine')
			.attr('x1', function(d, i) {

				return d.x
			})
			.attr('y1', function(d, i) {

				return d.y
			})
			.attr('x2', function(d, i) {

				return d.x1
			})
			.attr('y2', function(d, i) {

				return d.y1
			})
			.attr('stroke', function(d, i) {

				return d.color
			})
			.attr('stroke-width', setting.lightLine.strokeWidth)

		lineBold.selectAll('.boldLine')
			.data(lineArrayBold)
			.enter()
			.append('line')
			.attr('class', 'boldLine')
			.attr('x1', function(d, i) {

				return d.x
			})
			.attr('y1', function(d, i) {

				return d.y
			})
			.attr('x2', function(d, i) {

				return d.x1
			})
			.attr('y2', function(d, i) {

				return d.y1
			})
			.attr('stroke', function(d, i) {

				return d.color
			})
			.attr('stroke-width', setting.boldLine.strokeWidth)

		textName.selectAll('.textName')
			.data(lineArrayBold)
			.enter()
			.append('text')
			.attr('class', 'textName')
			.attr('x', function(d, i) {

				if (d.x > 0) {

					return d.x
				} else {

					return d.x1
				}
			})

		.attr('y', function(d, i) {

				return d.y
			})
			.attr('fill', function(d, i) {

				if (d.x < 0) {

					return nameStyle.color
				} else {

					return d.color
				}
			})
			.attr('dx', function(d, i) {

				if (d.x < 0) {

					return '0.1em'
				} else {

					return '0.5em'
				}

			})
			.attr('dy', '-0.15em')
			.text(function(d, i) {

				if (d.x < 0) {

					return d.name
				} else {

					return d.value + '%'
				}

			})
			.style('font-size', function(d, i) {

				if (d.x < 0) {

					return nameStyle.fontSize
				} else {

					return valueStyle.fontSize
				}

			})
			.style('font-weight', valueStyle.fontWeight)

		textValue.selectAll('.textValue')
			.data(lineArrayLight)
			.enter()
			.append('text')
			.attr('class', 'textValue')
			.attr('x', function(d, i) {

				if (d.x > 0) {

					return d.x
				} else {

					return d.x1
				}
			})
			.attr('y', function(d, i) {

				return d.y
			})
			.attr('fill', function(d, i) {

				if (d.x > 0) {

					return valueStyle.color
				} else {

					return d.color
				}
			})
			.attr('dx', '0.35em')
			.attr('dy', '-0.15em')
			.text(function(d, i) {

				if (d.x < 0) {

					return d.value + '%'
				} else {

					return d.name
				}

			})
			.style('font-size', function(d, i) {

				if (d.x < 0) {

					return valueStyle.fontSize
				} else {

					return nameStyle.fontSize
				}

			})
			.style('font-weight', valueStyle.fontWeight)

		con.append('text')
			.text(text)
			.attr('x', 0)
			.attr('y', -(textRect.height * 1 / 2))
			.style(textStyle)
		con.append('text')
			.text(subtext)
			.attr('x', 0)
			.attr('y', subtextRect.height)
			.style(textStyle)

	}

	// 后面为引用

	window.Y = Y

}())